home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8094 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  57 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.netins.net!isac!gg
  3. From: gg@isac.hces.com (Greg Goodrich)
  4. Subject: Re: Using char array in functions
  5. Message-ID: <1996Feb29.230019.5028@isac.hces.com>
  6. Organization: Health Care Expert Systems
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <20FEB199609155775@sundog.caltech.edu> <4gqk5m$jno@aphex.direct.ca>
  9. Date: Thu, 29 Feb 1996 23:00:19 GMT
  10.  
  11. Ed Toivanen (etoivane@direct.ca) wrote:
  12. : In article <20FEB199609155775@sundog.caltech.edu>, 
  13. : taylor@sundog.caltech.edu395-3807,MC264-33CALTECHPASADENACA91125 says...
  14. : >
  15. : >I've searched the faq's and postings, but still am looking for a clear 
  16. : >explanation on how to use arrays of char strings in functions, especially when 
  17. : >I want to have the function modify the array.  I find I can't just pass the 
  18. : >name of the string as a pointer as I do for single strings.
  19. :  ^^^^
  20.  
  21. : As I just learned from my midterm(the hard way, in other words!) you have to 
  22. : pass the address of the name of the array if you expext to modify more than a 
  23. : copy of the thing.  
  24.  
  25. Take the following example:
  26.  
  27. /* prototype for function */
  28. void foo(char *bar);
  29.  
  30. /* declaration */
  31. char array1[100];
  32.  
  33. main()
  34. {
  35.     foo(array1);
  36. }
  37.  
  38. In this example, the function foo() can modify any member of the array
  39. "array1" at will.  You do not have to pass the address of the array
  40. name, because by using the array name, you are actually passing
  41. &array1[0], or in english, the address of the first member of the array,
  42. which for argument's sake is also the address of the array.  This allows
  43. any manipulation of the array inside the function that the function
  44. wishes.  As a matter of fact, passing a copy of an array into a function
  45. is the real challenge, and is usually done in a roundabout sort of way
  46. by placing the array inside a structure and passing the entire structure
  47. as a parameter to the function.  Another method is to declare two
  48. arrays, and before calling the function, copy the first array into the
  49. second by using something like memcpy().
  50.  
  51. Greg.
  52. -- 
  53. _______________________________________
  54.     Greg Goodrich - gg@hces.com
  55.     Software Engineer
  56.     PACE Health Management Systems
  57.